<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" size="small"  v-model="queryInfo.username" class="input-with-select" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" size="small" @click="addArticle">添加新闻</el-button>
        </el-col>
      </el-row>
      <el-table :data="articleList" border stripe v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading">
        <el-table-column label="新闻封面图">
          <template slot-scope="scope">
            <el-popover
                placement="right"
                :title="scope.row.title"
                trigger="hover">
              <img :src="scope.row.img" style="max-height: 200px;max-width: 200px" />
              <img slot="reference" :src="scope.row.img" style="max-height: 50px;max-width: 50px" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="新闻名称"></el-table-column>
        <el-table-column prop="author" label="作者"></el-table-column>
        <el-table-column prop="source" label="来源"></el-table-column>
        <el-table-column prop="column_name" label="栏目"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="delArticle(scope.row.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[10, 20, 30, 40,50]"
          :page-size="queryInfo.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'index',
  data(){
    return{
      queryInfo:{
        title:'',
        limit: this.$global_limit,
        page: this.$global_page
      },
      articleList:[],
      loading:true,
      total: 0,
    }
  },
  created () {
    sessionStorage.removeItem('article_id')
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      const {data: res} = await this.$http.get('dashboard/article_list', {params: this.queryInfo})
      if (res.code !== 200) return this.$message.error(res.data.message)
      this.articleList = res.data.list
      this.total = res.data.totalCount
      this.loading = false
    },
    //新闻跳转页面
    addArticle () {
      this.$router.push('/dashboard/article/add')
    },
    handleSizeChange (newSize) {
      this.queryInfo.limit = newSize
      this.getArticleList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getArticleList()
    },
    showEditDialog (id) {
      this.article_id = sessionStorage.setItem('article_id', id)
      this.$router.push({name: '新增新闻'})
    },
    //删除新闻
    async delArticle(id) {
      const configResult = await this.$confirm('此操作将永久删除该新闻, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err);
      if (configResult !== 'confirm') return this.$message.info('已取消删除')
      const {data: result} = await this.$http.delete('dashboard/del_article/' + id)
      if (result.code !== 200) return this.$message.error(result.data.message)
      this.$message.success('删除成功')

      //删除成功后页码改变返还为上一页
      let totalPage = Math.ceil((this.total - 1) / this.queryInfo.limit);
      let currentPage = this.queryInfo.page > totalPage ? totalPage : this.queryInfo.page;
      this.queryInfo.page = this.queryInfo.page < 1 ? 1 : currentPage;

      this.getArticleList();
    }
  }
}
</script>

<style scoped>

</style>
